package com.tjl.foodapp.utils;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.date.DateUtil;

/**
 * @author tjl
 */
public class TimeUtil {

    /**
     * 跟据当前时间生成四小时内的时间选项
     * @param spinner 下拉框
     */
    public static void generateTimeOptions(Spinner spinner) {

        // 生成四小时内的时间列表，每半小时为一个选项
        List<String> timeOptions = new ArrayList<>();
        LocalDateTime now = LocalDateTime.now();
        for (int i = 0; i < 4; i++) {
            LocalDateTime startTime = now.plusMinutes(i * 30);
            LocalDateTime endTime = startTime.plusMinutes(30);
            timeOptions.add(startTime.format(DateTimeFormatter.ofPattern("HH:mm")) + " - " + endTime.format(DateTimeFormatter.ofPattern("HH:mm")));
        }

        // 将时间选项设置为下拉框的选项
        ArrayAdapter<String> adapter = new ArrayAdapter<>(spinner.getContext(), android.R.layout.simple_spinner_item, timeOptions);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
    }


}
